package com.leyou.page.service.impl;

import com.leyou.item.clients.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.dto.SpecGroupNameDTO;
import com.leyou.page.dto.SpecParamNameDTO;
import com.leyou.page.service.GoodsPageService;
import com.leyou.utils.BeanHelper;
import com.leyou.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/15 22:33
 * @description：
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private StringRedisTemplate redisTemplate;




    @Override
    public String loadSpuData(Long spuId) {

        SpuDTO spu = itemClient.queryGoodsById(spuId);
        Map<String, Object> map = new HashMap<>();
        map.put("id", spu.getId());
        map.put("name", spu.getName());
        map.put("categoryIds", spu.getCategoryIds());
        map.put("brandId", spu.getBrandId());
        String json = JsonUtils.toJson(map);
        redisTemplate.opsForValue().set("page:spu:id:" + spuId, json);
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {

        SpuDetailDTO spuDetail = itemClient.querySpuDetailBySpuId(spuId);
        String json = JsonUtils.toJson(spuDetail);
        redisTemplate.opsForValue().set("page:detail:id:" + spuId, json);
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        List<SkuDTO> skuList = itemClient.querySkuBySpuId(spuId);
        String json = JsonUtils.toJson(skuList);
        redisTemplate.opsForValue().set("page:sku:id:" + spuId, json);
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        List<CategoryDTO> list = itemClient.queryCategoryByIdList(ids);
        List<HashMap<String, Object>> categoryList = list.stream().map(
                categoryDTO -> {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("id", categoryDTO.getId());
                    map.put("name", categoryDTO.getName());
                    return map;
                }
        ).collect(Collectors.toList());

        String json = JsonUtils.toJson(categoryList);
        redisTemplate.opsForValue().set("page:category:id:" + ids.get(2), json);
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {

        BrandDTO brand = itemClient.queryBrandById(brandId);
        Map<String, Object> map = new HashMap<>();
        map.put("id", brand.getId());
        map.put("name", brand.getName());
        String json = JsonUtils.toJson(map);
        redisTemplate.opsForValue().set("page:brand:id:" + brandId, json);
        return json;
    }

    @Override
    public String loadSpecData(Long categoryId) {
        // 查询信息
        List<SpecGroupDTO> list = itemClient.querySpecGroupAndParamByCategoryId(categoryId);

        List<SpecGroupNameDTO> groupList = new ArrayList<>();
        for (SpecGroupDTO groupDTO : list) {
            SpecGroupNameDTO nameDTO = new SpecGroupNameDTO();
            nameDTO.setName(groupDTO.getName());
            nameDTO.setParams(BeanHelper.copyWithCollection(groupDTO.getParams(), SpecParamNameDTO.class));
            groupList.add(nameDTO);
        }

        String json = JsonUtils.toJson(groupList);
        // 存入Redis
        redisTemplate.opsForValue().set("page:spec:id:" + categoryId, json);
        return json;
    }
}
